package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.ExcelSubjectData;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.handler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * @Auther: fangshicai
 * @Date: 2022/1/25 22:29
 * @Description: 监听器无法交给spring容器管理 所以传入eduSubjectService方便数据操作
 */

public class MySubjectEventListener extends AnalysisEventListener<ExcelSubjectData> {

    EduSubjectService eduSubjectService;
    //构造器
    public MySubjectEventListener() {}
    public MySubjectEventListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService=eduSubjectService;
    }



    //读取excel表格每一行 放入subjectData对象中
    @Override
    public void invoke(ExcelSubjectData subjectData, AnalysisContext analysisContext) {

        if(subjectData==null){
            //excel表格为空时
            throw new MyException(20001,"添加失败");
        }else {
            /**
             * excel表格不为空时
             * 如果在数据库中一级课程没有，则添加课程
            **/
            EduSubject existOneEduSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
            if(existOneEduSubject==null){
                existOneEduSubject=new EduSubject();
                existOneEduSubject.setTitle(subjectData.getOneSubjectName());
                existOneEduSubject.setParentId("0");
                eduSubjectService.save(existOneEduSubject);
            }

            //获取一级分类id值
            String Pid = existOneEduSubject.getId();

            //添加二级分类
            EduSubject existTwoEduSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(),Pid);
            if(existTwoEduSubject==null){
                existTwoEduSubject=new EduSubject();
                existTwoEduSubject.setTitle(subjectData.getTwoSubjectName());
                existTwoEduSubject.setParentId(Pid);
                eduSubjectService.save(existTwoEduSubject);
            }

        }
    }

    //一级分类查找 从数据库中查找一级课程中有没有重名的
    public EduSubject existOneSubject( EduSubjectService eduSubjectService,String name){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<EduSubject>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);

        return  oneSubject;
    }

    //二级分类查找
    public EduSubject existTwoSubject( EduSubjectService eduSubjectService,String name ,String Pid){
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<EduSubject>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",Pid);
        EduSubject TwoSubject = eduSubjectService.getOne(wrapper);

        return  TwoSubject;
    }

    //查询完成后
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
